package su.myspringapps.onlineschool.repository;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import su.myspringapps.onlineschool.entity.Teacher;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
class TeacherRepositoryTest {

    @Autowired
    TeacherRepository teacherRepository;

    @Test
    void testCreate() throws Exception {
        Teacher expectedTeacher = new Teacher();
        expectedTeacher.setName("Шантор Червиз");
        expectedTeacher.setSalary(15550);
        expectedTeacher.setAge(22);
        Integer id = teacherRepository.create(expectedTeacher);
        expectedTeacher.setCourses(new ArrayList<>());
        Teacher actualTeacher = teacherRepository.findById(id);
        Assertions.assertEquals(expectedTeacher, actualTeacher);
        teacherRepository.delete(expectedTeacher);
        actualTeacher = teacherRepository.findById(id);
        Assertions.assertNull(actualTeacher);

    }

    @Test
    void testUpdate() throws Exception {
        Teacher newTeacher = new Teacher();
        newTeacher.setName("Валя Полотер");
        newTeacher.setSalary(666740);
        newTeacher.setAge(24);
        Integer id = teacherRepository.create(newTeacher);
        newTeacher.setCourses(new ArrayList<>());
        Teacher teacherForUpdate = teacherRepository.findById(id);
        Assertions.assertEquals(newTeacher, teacherForUpdate);
        String newName = "Таня Чумовоз";
        teacherForUpdate.setName(newName);
        teacherRepository.update(teacherForUpdate);
        Teacher actualTeacher = teacherRepository.findById(id);
        Assertions.assertNotEquals(newTeacher, actualTeacher);
        Assertions.assertEquals(newName, actualTeacher.getName());
        teacherRepository.delete(actualTeacher);
        Assertions.assertNull(teacherRepository.findById(id));
    }

    @Test
    void testDelete() throws Exception {
        Teacher teacherForDelete = new Teacher();
        teacherForDelete.setName("Шантор Червиз");
        teacherForDelete.setSalary(740666);
        teacherForDelete.setAge(23);
        Integer id = teacherRepository.create(teacherForDelete);
        teacherForDelete.setCourses(new ArrayList<>());
        Teacher actualTeacher = teacherRepository.findById(id);
        Assertions.assertEquals(teacherForDelete, actualTeacher);
        teacherRepository.delete(teacherForDelete);
        actualTeacher = teacherRepository.findById(id);
        Assertions.assertNull(actualTeacher);
    }

    @Test
    void testFindById() throws Exception {
        Teacher actualTeacher = teacherRepository.findById(1);
        Assertions.assertEquals("Ягешев Сидор", actualTeacher.getName());
        Assertions.assertEquals(10000, actualTeacher.getSalary());
        Assertions.assertEquals(18, actualTeacher.getAge());
    }

    @Test
    void testFindAll() throws Exception {
        List<Teacher> teacherList = teacherRepository.findAll();
        Teacher actualTeacher1 = teacherList.get(0);
        Teacher actualTeacher2 = teacherList.get(1);
        Assertions.assertEquals(1, actualTeacher1.getId());
        Assertions.assertEquals("Ягешев Сидор", actualTeacher1.getName());
        Assertions.assertEquals(10000, actualTeacher1.getSalary());
        Assertions.assertEquals(18, actualTeacher1.getAge());
        Assertions.assertEquals(2, actualTeacher2.getId());
        Assertions.assertEquals("Яглинцев Владислав", actualTeacher2.getName());
        Assertions.assertEquals(20000, actualTeacher2.getSalary());
        Assertions.assertEquals(19, actualTeacher2.getAge());
    }

}